﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

namespace BuyTrainTicket
{
    public partial class SelectPassengers : PhoneApplicationPage
    {
        private string[] cardtypevalue = new string[] { "1", "2", "C", "G", "B" };
        public SelectPassengers()
        {
            InitializeComponent();
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            string contacts;
            settings.TryGetValue<String>("contacts", out contacts);
            if (contacts != null && !contacts.Equals(""))
            {
                JObject responseObj = JsonConvert.DeserializeObject<JObject>(contacts);
                JObject respHtml = (JObject)responseObj.GetValue("respHtml");
                JArray contactsArray = (JArray)respHtml.GetValue("rows");
                List<Item> items = new List<Item>();
                foreach (JToken contact in contactsArray)
                {
                    String passenger_name = (String)((JObject)contact).GetValue("passenger_name");
                    String sex_name = (String)((JObject)contact).GetValue("sex_name");
                    String passenger_id_type_code = (String)((JObject)contact).GetValue("passenger_id_type_code");
                    String passenger_id_no = (String)((JObject)contact).GetValue("passenger_id_no");
                    String mobile_no = (String)((JObject)contact).GetValue("mobile_no");
                    String passenger_type_name = (String)((JObject)contact).GetValue("passenger_type_name");
                    Item item = new Item(passenger_name + "," + passenger_id_type_code + "," + passenger_id_no);
                    items.Add(item);
                }
                Dispatcher.BeginInvoke(() => { passengerList.ItemsSource = items; });
            }
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings["passengers"] = text_passengers.Text;
            settings.Save();
            base.OnBackKeyPress(e);
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            string name = text_name.Text;
            string cardtype = cardtypevalue[list_cardtype.SelectedIndex];
            string cardno = text_cardno.Text;
            if (name == null || name.Equals("") || cardtype == null || cardtype.Equals("") || cardno == null || cardno.Equals(""))
            {
                MessageBox.Show("姓名 证件信息请输入");
                return;
            }
            string tmp = text_passengers.Text;
            string str = name+","+cardtype+","+cardno;
            if (tmp== null || tmp.Equals(""))
            {
                text_passengers.Text = str;
            }
            else
            {
                text_passengers.Text = tmp + ";" + str;
            }
        }

        private void clear_Click(object sender, RoutedEventArgs e)
        {
            text_passengers.Text = "";
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            String passengersstr;
            settings.TryGetValue<String>("passengers", out passengersstr);
            if (passengersstr != null)
            {
                text_passengers.Text = passengersstr;
            }
        }

        private void ContentControl_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Item item = ((FrameworkElement)sender).DataContext as Item;
            string tmp = text_passengers.Text;
            if (tmp == null || tmp.Equals(""))
            {
                text_passengers.Text = item.ItemContent;
            }
            else
            {
                text_passengers.Text = tmp + ";" + item.ItemContent;
            }
        }
    }
}